package org.thotheolh.sweatkeeper.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

import com.toedter.calendar.JCalendar;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class NewWorkoutDialog extends JDialog {

	private JPanel contentPanel = new JPanel();
	private JPanel buttonPane = new JPanel();
	private JTextField saveTf;
	private Calendar currentTime = new GregorianCalendar();
	private JLabel dateLbl;
	private JLabel hrLbl;
	private JLabel timeLbl;
	private JLabel minLbl;
	private JLabel whenToLbl;
	private JLabel saveLbl;
	private JCalendar calendar;
	private JSpinner hrSp;
	private JSpinner minSp;
	private JRadioButton workoutNowRb;
	private JRadioButton saveWorkoutRb;
	private JButton clearBtn;
	private JButton cancelBtn;
	private JButton okBtn;
	private ButtonGroup btnGrp;
	private JLabel workoutLbl;
	private JComboBox workoutTypeCb;
	
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			NewWorkoutDialog dialog = new NewWorkoutDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public NewWorkoutDialog() {
		setTitle("New Workout");
		setBounds(100, 100, 450, 409);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		btnGrp = new ButtonGroup();
		contentPanel.setLayout(new MigLayout("", "[][][grow][][][grow][][][][]", "[][][][][][][][]"));
		{
			dateLbl = new JLabel("Workout Date: ");
			contentPanel.add(dateLbl, "cell 0 0");
		}
		{
			calendar = new JCalendar();
			calendar.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			contentPanel.add(calendar, "cell 0 1");
		}
		{
			timeLbl = new JLabel("Workout Time: ");
			contentPanel.add(timeLbl, "cell 5 0");
		}
		{
			hrSp = new JSpinner();
			hrSp.setModel(new SpinnerNumberModel(0, 0, 23, 1));
			contentPanel.add(hrSp, "flowx,cell 5 1,aligny top");
		}
		{
			hrLbl = new JLabel("hr");
			contentPanel.add(hrLbl, "cell 5 1,aligny top");
		}
		{
			minSp = new JSpinner();
			minSp.setModel(new SpinnerNumberModel(0, null, 59, 1));
			contentPanel.add(minSp, "cell 5 1,aligny top");
		}
		{
			minLbl = new JLabel("min");
			contentPanel.add(minLbl, "cell 5 1,aligny top");
		}
		{
			whenToLbl = new JLabel("Choose when to workout: ");
			contentPanel.add(whenToLbl, "cell 0 3");
		}
		{
			workoutLbl = new JLabel("Workout type: ");
			contentPanel.add(workoutLbl, "cell 5 3");
		}
		{
			workoutNowRb = new JRadioButton("Workout NOW !");
			workoutNowRb.setSelected(true);
			contentPanel.add(workoutNowRb, "cell 0 4");
			btnGrp.add(workoutNowRb);
		}
		{
			workoutTypeCb = new JComboBox();
			workoutTypeCb.setModel(new DefaultComboBoxModel(new String[] {"Gym Workout", "Running"}));
			contentPanel.add(workoutTypeCb, "cell 5 4,growx");
		}
		{
			saveWorkoutRb = new JRadioButton("Save workout for later...");
			contentPanel.add(saveWorkoutRb, "cell 0 5");
			btnGrp.add(saveWorkoutRb);
		}
		{
			saveLbl = new JLabel("       Save as: ");
			saveLbl.setEnabled(false);
			contentPanel.add(saveLbl, "flowx,cell 0 6");
		}
		{
			saveTf = new JTextField();
			saveTf.setEnabled(false);
			contentPanel.add(saveTf, "cell 0 6");
			saveTf.setColumns(18);
		}
		{
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				clearBtn = new JButton("Clear");
				buttonPane.add(clearBtn);
			}
			{
				cancelBtn = new JButton("Cancel");
				cancelBtn.setActionCommand("Cancel");
				buttonPane.add(cancelBtn);
			}
			{
				okBtn = new JButton("Create Workout");
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
		}
		initTime();
		initBtnGrpAction();
	}
	
	public void initTime(){
		hrSp.setValue(currentTime.get(Calendar.HOUR_OF_DAY));	
		minSp.setValue(currentTime.get(Calendar.MINUTE));
	}
	
	public void initBtnGrpAction(){		
		workoutNowRb.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				saveLbl.setEnabled(false);
				saveTf.setEnabled(false);
			}
		});
		
		saveWorkoutRb.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				saveLbl.setEnabled(true);
				saveTf.setEnabled(true);
			}
		});
	}

}
